package com.gonsin.gview.common.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gonsin.gview.common.bean.EdificeBean;
import com.gonsin.gview.common.po.EdificeSearchPo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.domain.Pageable;

import java.util.List;

@Mapper
public interface EdificeMapper extends BaseMapper<EdificeBean> {


    /**
     * 搜索会务中心
     */

    @Select("<script>" +
            "select * " +
            "from gs_user_edifice as g " +
            "where 1=1 " +
            "<if test =\"searchPo.title != null &amp;&amp; searchPo.title != ''\"> " +
            "   and g.title like concat('%', #{searchPo.title},'%')" +
            "</if> " +
            "<if test =\"searchPo.host != null &amp;&amp; searchPo.host != ''\"> " +
            "   and g.host like concat('%', #{searchPo.host},'%')" +
            "</if> " +
            "<if test =\"searchPo.edificeKeys != null &amp;&amp; searchPo.edificeKeys.size() &gt; 0\"> " +
            "   and g.edifice_key in " +
            "   <foreach item=\"item\" index=\"index\" collection=\"searchPo.edificeKeys\" separator=\",\" open=\"(\" close=\")\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if> " +
            "<if test=\"!pageable.sort.toString().equals('UNSORTED')\"> " +
            "order by g." +
            "<foreach item=\"item\" index=\"index\" collection=\"pageable.sort.orders\" separator=\",\"> " +
            "${item.property} ${item.direction} " +
            "</foreach>" +
            "</if>" +
            "limit ${pageable.page * pageable.size},${pageable.size} " +
            "</script>")
    List<EdificeBean> searchEdifices(@Param("searchPo") EdificeSearchPo searchPo, @Param("pageable") Pageable pageable);


    @Select("<script>" +
            "select ifnull(count(g.edifice_key), 0)" +
            "from gs_user_edifice as g " +
            "where 1=1 " +
            "<if test =\"searchPo.title != null &amp;&amp; searchPo.title != ''\"> " +
            "   and g.title like concat('%', #{searchPo.title},'%')" +
            "</if> " +
            "<if test =\"searchPo.host != null &amp;&amp; searchPo.host != ''\"> " +
            "   and g.host like concat('%', #{searchPo.host},'%')" +
            "</if> " +
            "<if test =\"searchPo.edificeKeys != null &amp;&amp; searchPo.edificeKeys.size() &gt; 0\"> " +
            "   and g.edifice_key in " +
            "   <foreach item=\"item\" index=\"index\" collection=\"searchPo.edificeKeys\" separator=\",\" open=\"(\" close=\")\">" +
            "       #{item}" +
            "   </foreach>" +
            "</if> " +
            "</script>")
    int countEdifices(@Param("searchPo") EdificeSearchPo searchPo);

    @Select("select e.module_settings from gs_user_edifice as e where e.edifice_key = #{edificeKey}")
    String getModuleSetting(@Param("edificeKey") String edificeKey);

    @Update("update gs_user_edifice as e set e.module_settings = #{json} where e.edifice_key = #{edificeKey}")
    void updateModuleSetting(@Param("edificeKey") String edificeKey, @Param("json") String json);

    @Select("select e.* from gs_user_edifice as e where e.mini_appid = #{miniAppId}")
    EdificeBean getEdificeByAppid(@Param("miniAppId") String miniAppId);
}
